﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Model.WXshopBuiness;
using Model.WXshopBuiness.Enum;
using Model.WeiXin.DTO;
using Services.WXshopBuiness;
using Xproject.Util;
using Model.Const;
using Services.Util;
using Model.Util;
using Model.Util.DTO;
using Model.Finance.DTO;
using Services.Finance;
using Services.WeiXinProject;
using Services.ZXBuiness;
using Model.ZXBuiness;

namespace WeiXinProject.Controllers
{
    public class UserCenterController : WXBaseController
    {
        WXOrderServices m_WXOrderServices;
        UserOperateServices m_UserOperateServices;
        UserCollectionServices m_userCollectionServices;
        AccountBalanceServices m_AccountBalanceServices;
        ZXUserInfoServives m_zxUserInfoServices;
        UserInfoServices m_UserInfoServices;
        public UserCenterController(UserInfoServices mUserInfoServices,ZXUserInfoServives mzxUserInfoServices ,AccountBalanceServices mAccountBalanceServices,WXOrderServices mWXOrderServices, UserOperateServices mUserOperateServices, UserCollectionServices muserCollectionServices)
        {
            m_WXOrderServices = mWXOrderServices;
            m_UserOperateServices = mUserOperateServices;
            m_userCollectionServices = muserCollectionServices;
            m_AccountBalanceServices = mAccountBalanceServices;
            m_zxUserInfoServices =   mzxUserInfoServices;
            m_UserInfoServices = mUserInfoServices;
        }

        public ActionResult BindInfo()
        {            
            return View(this.CurrentUser);
        }
        [HttpPost]
        public JsonResult BindMyInfo()
        {
            var jsonData = new Object();
            string msg = "";
            string Phone = Request.Form["Phone"];
            string WeiXin = Request.Form["WeiXin"];
            string ZhiFuBao = Request.Form["ZhiFuBao"];
            string QQ = Request.Form["QQ"];
            long ZX_UserId = this.CurrentUser.ZX_UserID.Value;
            string bankCard = Request.Form["bankCard"];
            ZXUserInfo zx =  m_zxUserInfoServices.GetByZXUserId(ZX_UserId);
            zx.BindPhone = Phone;
            zx.BindBankCard = bankCard;
            zx.BindQQ = QQ;
            zx.BindWeiXin = WeiXin;
            zx.BindZhiFuBao = ZhiFuBao;
            bool success= m_zxUserInfoServices.UpdateZXUserInfo(zx);
            if (success)
            {
                ZXUserInfoModel zxuser = m_UserInfoServices.GetWeiXinZXUserInfoByOpenId(this.CurrentUser.OpenId);
                this.SetSession(zxuser, this.CurrentUser.OpenId);//重置登录信息
            }
            msg = success ? "更新成功" : "更新失败";
            jsonData = new { Success = success, Data = msg };
            return Json(jsonData, JsonRequestBehavior.AllowGet);
        }
        public ActionResult UserCenter()
        {
            ViewBag.AdminCQ =( this.CurrentUser.Mobile == "X18211436586X");
            string userId = this.CurrentUser.UserID;
            var usermodel = Cache_UserInfoModel.GetUserInfoModel(userId);
            bool isSalesman = false;
            if (this.CurrentUser.ZX_UserType.HasValue)
            {
                isSalesman = ( (this.CurrentUser.ZX_UserType == (int)EnumZX_UserType.SYSTEM_SALES) ? true : false);
            }
            ViewBag.isSalesman = isSalesman;
            if (usermodel == null)
            {
                usermodel = new UserInfoModel();
                usermodel.UserID = userId;
                usermodel.UserCode = this.CurrentUser.UserCode;
                usermodel.HeadUrl = this.CurrentUser.HeadUrl;
                usermodel.NickName = this.CurrentUser.NickName;
                usermodel.orderinfo = new UserOrderInfo();               
                usermodel.orderinfo.ShopCarNeedToPayCount = m_WXOrderServices.GetUserShoppingCarCount(userId);
                usermodel.orderinfo.ShopCarNeedToPayTotal = m_WXOrderServices.GetUserShoppingCarMoney(userId);
                List<WeiXinOrder> orders = m_WXOrderServices.GetUserOrderList(userId, (int)Enum_OrderState.WAITINGPAY);
                usermodel.orderinfo.NeedToPayCount = orders.Count;               
                orders = m_WXOrderServices.GetUserOrderList(userId, (int)Enum_OrderState.PAYED);
                usermodel.orderinfo.ShopCarPayedSuccessCount = orders.Count;
                orders = m_WXOrderServices.GetUserOrderList(userId, (int)Enum_OrderState.SHIPPING);
                usermodel.orderinfo.ShopCarShippingCount = orders.Count;
                orders = m_WXOrderServices.GetUserOrderList(userId, (int)Enum_OrderState.FINISHED);
                usermodel.orderinfo.ShopCarDoneCount = orders.Count;
                orders = m_WXOrderServices.GetUserOrderList(userId, (int)Enum_OrderState.WAITCOMMENT);
                usermodel.orderinfo.ShopCarWaitingComment = orders.Count;
                Cache_UserInfoModel.SetUserInfoModelCache(userId, usermodel);
            }
            else
            {
                usermodel.UserID = userId;
                usermodel.UserCode = this.CurrentUser.UserCode;
                usermodel.HeadUrl = this.CurrentUser.HeadUrl;
                usermodel.NickName = this.CurrentUser.NickName;
                usermodel.Mobile = this.CurrentUser.Mobile;
                if (usermodel.orderinfo == null)
                {
                    usermodel.orderinfo = new UserOrderInfo();                    
                    usermodel.orderinfo.ShopCarNeedToPayCount = m_WXOrderServices.GetUserShoppingCarCount(userId);
                    usermodel.orderinfo.ShopCarNeedToPayTotal = m_WXOrderServices.GetUserShoppingCarMoney(userId);
                    List<WeiXinOrder> orders = m_WXOrderServices.GetUserOrderList(userId, (int)Enum_OrderState.WAITINGPAY);
                    usermodel.orderinfo.NeedToPayCount = orders.Count;
                    usermodel.orderinfo.ShopCarNeedToPayTotal = orders.Select(m => m.OrderMoney).Sum();
                    orders = m_WXOrderServices.GetUserOrderList(userId, (int)Enum_OrderState.PAYED);
                    usermodel.orderinfo.ShopCarPayedSuccessCount = orders.Count;
                    orders = m_WXOrderServices.GetUserOrderList(userId, (int)Enum_OrderState.SHIPPING);
                    usermodel.orderinfo.ShopCarShippingCount = orders.Count;
                    orders = m_WXOrderServices.GetUserOrderList(userId, (int)Enum_OrderState.FINISHED);
                    usermodel.orderinfo.ShopCarDoneCount = orders.Count;
                    orders = m_WXOrderServices.GetUserOrderList(userId, (int)Enum_OrderState.WAITCOMMENT);
                    usermodel.orderinfo.ShopCarWaitingComment = orders.Count;
                    Cache_UserInfoModel.SetUserInfoModelCache(userId, usermodel);
                }
            }
            ViewBag.orderInfo = usermodel.orderinfo;
            ViewBag.basicInfo = usermodel;
            return View();
        }
        // GET: UserCenter
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult MyFoot()
        {
            ZXUserInfoModel userInfoModel = this.CurrentUser;         
            string userId = this.CurrentUser.UserID;
            var usermodel = this.CurrentUser;          
            ViewBag.basicInfo = usermodel;
            ViewBag.zxUserId = this.CurrentUser.ZX_UserID.HasValue ? CurrentUser.ZX_UserID.Value.ToString() : "0";
            return View();
        }

        public JsonResult GetMyfootHistory(int? index, int? pagesize)
        {
            var jsonData = new Object();
            List<UserOperateLogModel> model = new List<UserOperateLogModel>();
            model = m_UserOperateServices.GetMyBrowserHistory( index, pagesize,this.CurrentUser.UserID);
            jsonData = new { Success = true, Data = model };
            return Json(jsonData, JsonRequestBehavior.AllowGet);
        }


        public ActionResult MyCollection()
        {
            ZXUserInfoModel userInfoModel = this.CurrentUser;
            string userId = this.CurrentUser.UserID;
            var usermodel = this.CurrentUser;
            ViewBag.basicInfo = usermodel;
            ViewBag.zxUserId = this.CurrentUser.ZX_UserID.HasValue ? CurrentUser.ZX_UserID.Value.ToString() : "0";
            return View();
        }

        public JsonResult GetMyCollections(int? index, int pagesize)
        {
            var jsonData = new Object();
            List<UserCollection> model = new List<UserCollection>();
            model = m_userCollectionServices.GetMyCollections(index, pagesize, this.CurrentUser.UserID);
            jsonData = new { Success = true, Data = model };
            return Json(jsonData, JsonRequestBehavior.AllowGet);
        }


        public ActionResult MyAccountBalance(string  BalanceCode)
        {
            ZXUserInfoModel userInfoModel = this.CurrentUser;
            string userId = this.CurrentUser.UserID;
            var usermodel = this.CurrentUser;
            ViewBag.basicInfo = usermodel;
            ViewBag.zxUserId = this.CurrentUser.ZX_UserID.HasValue ? CurrentUser.ZX_UserID.Value.ToString() : "0";
            ViewBag.BalanceCode = BalanceCode;
            return View();
        }

        public JsonResult GetMyAccountBalances(int? index, int pagesize,string BalanceCode)
        {
            var jsonData = new Object();
            List<AccountBalanceModel> model = new List<AccountBalanceModel>();
            model = m_AccountBalanceServices.GetMyAccountBalanceList(index, pagesize, this.CurrentUser.UserID,BalanceCode);
            jsonData = new { Success = true, Data = model };
            return Json(jsonData, JsonRequestBehavior.AllowGet);
        }

        public ActionResult ClearCacheP()
        {
            var jsonData = new Object();
            Cache_Product.ClearWXProduct();
            Cache_Product.ClearWXProductTuiJian();
            jsonData = new { Success = true, Data = "OK" };
            return Json(jsonData, JsonRequestBehavior.AllowGet);
      
        }
    }
}